#!/bin/bash

# -------------------------------------------------------------------------- #
# Copyright 2002-2023, OpenNebula Project, OpenNebula Systems                #
#                                                                            #
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
# not use this file except in compliance with the License. You may obtain    #
# a copy of the License at                                                   #
#                                                                            #
# http://www.apache.org/licenses/LICENSE-2.0                                 #
#                                                                            #
# Unless required by applicable law or agreed to in writing, software        #
# distributed under the License is distributed on an "AS IS" BASIS,          #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
# See the License for the specific language governing permissions and        #
# limitations under the License.                                             #
#--------------------------------------------------------------------------- #

if [ -z "${ONE_LOCATION}" ]; then
    MAD_LOCATION=/usr/lib/one/mads
    VAR_LOCATION=/var/lib/one
    RUN_LOCATION=/var/run/one
    LOCK_LOCATION=/var/lock/one/
    LOG_LOCATION=/var/log/one
else
    MAD_LOCATION=$ONE_LOCATION/lib/mads
    VAR_LOCATION=$ONE_LOCATION/var
    RUN_LOCATION=$ONE_LOCATION/var/run
    LOCK_LOCATION=$ONE_LOCATION/var/lock
    LOG_LOCATION=$ONE_LOCATION/var
fi

export DRIVER_NAME="one_im_sh_${BASH_ARGV##* }"

#-------------------------------------------------------------------------------
#  vCenter Monitor Service startup routine
#-------------------------------------------------------------------------------
# Explicitly execute the vCenter monitor driver initialization and run it in the
# background. This way we don't have to wait until OpenNebula issues the
# add_hosts commands to know about missing hosts
#-------------------------------------------------------------------------------
if [ "${BASH_ARGV##* }" = "vcenter" ]; then

    LOCK_FILE="$LOCK_LOCATION/vcenter_monitor"
    FIFO_PATH="$RUN_LOCATION/vcenter_monitor.fifo"
    VMON_PATH="$VAR_LOCATION/remotes/im/lib/vcenter_monitor.rb"
    LOG="$LOG_LOCATION/vcenter_monitor.log"

    # Sanitize previous instances
    VMON_PIDS=$(ps axuww | grep "ruby[^ ]* $VMON_PATH" | grep -v grep | awk '{print $2}')

    if [ -n "$VMON_PIDS" ]; then
        kill -9 $VMON_PIDS
    fi

    # Create server FIFO & lockfile
    rm -f $FIFO_PATH > /dev/null 2>&1
    mkfifo $FIFO_PATH

    touch $LOCK_FILE

    # Start & check vcenter monitor service
    /usr/bin/env ruby $VMON_PATH </dev/null &>$LOG &

    sleep 3

    VMON_PIDS=$(ps axuww | grep "ruby[^ ]* $VMON_PATH" | grep -v grep | awk '{print $2}')

    if [ -z "$VMON_PIDS" ]; then
        echo "Cannot start vcenter_monitor service"
        exit 1
    fi
fi

exec $MAD_LOCATION/one_im_exec -l $*
